import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

 

public class CropImage extends JFrame implements MouseListener, MouseMotionListener{

    int drag_status=0,c1,c2,c3,c4;
    int flg = 0;
    
    /*
    public static void main(String args[]){
    	new CropImage().start("c:\\project\\one\\4.jpg");
    }
   	*/
    
	public void start(String name){
		
		BufferedImage imgB = null;
		try {
			imgB = ImageIO.read(new File(name));} catch (IOException e) { }
		
	
	    ImagePanel im=new ImagePanel(name);
	    ImageIcon img = new ImageIcon(name);
	    JLabel cropImgLbl = new JLabel(img);
		cropImgLbl.setBounds(0, 0,imgB.getWidth(), imgB.getHeight());
		
	    this.add(cropImgLbl);
	    
        setLocation(400, 20);
	    this.setSize(imgB.getWidth()+20, imgB.getHeight()+42);
	    setVisible(true);
	    addMouseListener(this);
	    addMouseMotionListener( this ); 
	//    setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void draggedScreen()throws Exception	{
		
		Point p = this.getLocationOnScreen();

		int w = p.x+c1 - c3;
		int h = p.y+c2 - c4;
		
		w = w * -1;
		h = h * -1;
		Robot robot = new Robot();
		BufferedImage img = robot.createScreenCapture(new Rectangle(p.x+c1+1, p.y+c2+1,w-1,h-1));
		File save_path=new File("c:\\project\\crop\\Crop.jpg");
		ImageIO.write(img, "JPG", save_path);
		System.out.println("Cropped image saved successfully.");
		dispose();

	}
	
	@Override
	
	public void mouseClicked(MouseEvent arg0) {     
	
	}
	
	
	@Override
	
	public void mouseEntered(MouseEvent arg0) {     
	
	}
	 
	
	@Override
	
	public void mouseExited(MouseEvent arg0) {      
	
	}
	 
	
	@Override
	
	public void mousePressed(MouseEvent arg0) {
	
	    repaint();
	    c1=arg0.getX();
	    c2=arg0.getY();
	
	}
	
	 
	@Override
	
	public void mouseReleased(MouseEvent arg0) {
	    repaint();
		Point p = this.getLocationOnScreen();
	    if(drag_status==1){
	    	c3=p.x+arg0.getX();
	    	c4=p.y+arg0.getY();
	    	
	    	try{
	    		draggedScreen();
	    		}
	    	catch(Exception e){
	    		e.printStackTrace();
	    		}
	    	}
	}
	
	 
	
	@Override
	
	public void mouseDragged(MouseEvent arg0){
		
		repaint();
	    drag_status=1;
	    c3=arg0.getX();
	    c4=arg0.getY();
	
	}
	
	
	@Override
	
	public void mouseMoved(MouseEvent arg0){
		  
	
	}
	
	 
	public void paint(Graphics g){
		
		super.paint(g);
	    int w = c1 - c3;
	    int h = c2 - c4;
	    w = w * -1;
	    h = h * -1;
	    if(w<0)
	    	w = w * -1;
	    g.setColor(Color.WHITE);
	    g.drawRect(c1, c2, w, h); 
	    }
}
